const conn = require('../app/database')

class CartModel {
	async getCartListByUid(uid) {
		const statement = `
      SELECT 
        gc.id cartId, gc.user_id userId, gc.goods_id goodsId, gc.goods_sku_id goodsSkuId, gc.goods_sku_desc goodsSkuDesc, gc.num num, gs.stock stock,
        JSON_OBJECT('spuNo', g.spu_no, 'goodsName', g.goods_name, 'showImg', g.show_img, 'initialPrice', g.initial_price, 'newPrice', new_price, 'categoryId', category_id, 'themeId', g.theme_id, 'goodsDesc', g.goods_desc) goodsObj
      FROM goods_cart gc 
      LEFT JOIN goods g ON g.id = gc.goods_id
      LEFT JOIN goods_sku gs ON gs.id = gc.goods_sku_id
      WHERE user_id = ?
      ORDER BY gc.id DESC;
    `
		const [result] = await conn.execute(statement, [uid])

		return result
	}

	async getCartGoods(uid, goodsId, goodsSkuId) {
		const statement = `
      SELECT 
        gc.id cartId, gc.user_id userId, gc.goods_id goodsId, gc.goods_sku_id goodsSkuId, gc.goods_sku_desc goodsSkuDesc, gc.num num
      FROM goods_cart gc
      WHERE user_id = ? AND goods_id = ? AND goods_sku_id = ?;
    `
		const [result] = await conn.execute(statement, [uid, goodsId, goodsSkuId])
		return result
	}

	async getCartGoodsByCartId(cartId) {
		const statement = `
      SELECT 
        gc.id cartId, gc.user_id userId, gc.goods_id goodsId, gc.goods_sku_id goodsSkuId, gc.goods_sku_desc goodsSkuDesc, gc.num num, gs.stock stock,
        JSON_OBJECT('spuNo', g.spu_no, 'goodsName', g.goods_name, 'showImg', g.show_img, 'initialPrice', g.initial_price, 'newPrice', new_price, 'categoryId', category_id, 'themeId', g.theme_id, 'goodsDesc', g.goods_desc) goodsObj
      FROM goods_cart gc
      LEFT JOIN goods g ON g.id = gc.goods_id
      LEFT JOIN goods_sku gs ON gs.id = gc.goods_sku_id
      WHERE gc.id = ?;
    `
		const [result] = await conn.execute(statement, [cartId])
		return result
	}

	async createCartGoodsRecord(uid, goodsId, goodsSKuId, goodsSkuContent, num) {
		const statement = `
      INSERT INTO goods_cart (user_id, goods_id, goods_sku_id, goods_sku_desc, num) VALUES (?,?,?,?,?);
    `

		const [result] = await conn.execute(statement, [
			uid,
			goodsId,
			goodsSKuId,
			goodsSkuContent,
			num,
		])
		return result
	}

	async updateCartGoodsNum(num, uid, goodsId, goodsSkuId) {
		const statement = `
      UPDATE goods_cart SET num = ?
      WHERE user_id = ? AND goods_id = ? AND goods_sku_id = ?;
    `
		const [result] = await conn.execute(statement, [
			num,
			uid,
			goodsId,
			goodsSkuId,
		])
		return result
	}

	async modifyCartGoodsNumByCartId(num, cartId) {
		const statement = `
      UPDATE goods_cart SET num = ? WHERE id = ?;
    `

		const [result] = await conn.execute(statement, [num, cartId])
		return result
	}

	async deleteCartGoodsByCartId(cartId) {
		const statement = `
      DELETE FROM goods_cart WHERE id = ?;
    `
		const [result] = await conn.execute(statement, [cartId])
		return result
	}

	async deleteAllCartGoods(uid) {
		const statement = `
    DELETE FROM goods_cart WHERE user_id = ?;
  `
		const [result] = await conn.execute(statement, [uid])
		return result
	}
}

module.exports = new CartModel()
